//
//  HOTMainViewController.m
//  Genpara
//
//  Created by AndyChang.zhang on 14-8-29.
//  Copyright (c) 2014年 zhangchh. All rights reserved.
//

#import "HOTMainViewController.h"
#import "HOTContributionHistoryListViewController.h"
#import "HOTMessageBoxViewController.h"
#import "HOTProfileReviewViewController.h"
#import "HOTMemoViewController.h"
#import "HOTProfileSettingViewController.h"
#import "HOTSettingViewController.h"

@interface HOTMainViewController ()

@end

@implementation HOTMainViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [_mainScrollView setFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    [_mainScrollView setContentSize:CGSizeMake(SCREEN_WIDTH, DESIGN_HEIGHT)];
    
    _nickLab.text = self.userInfo.nick;

    [self setNaviTitle:@"マイページ" fontSize:24 fontName:HOTTitleFont_ROUNDED_X];
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_bankruptcy"]];
    
    [self addRightBtnTarget:self action:@selector(settingBtnClicked) image:[UIImage imageNamed:@"btn_setting_off"] hImage:[UIImage imageNamed:@"btn_setting_on"]];
    
    [self selectBalance];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//查询余额
-(void)selectBalance {
    
    [self showLoading];
    
    NSDictionary *parameters = @{REQ_KEY_USER_TOKEN:self.userInfo.token,
                                 REQ_KEY_USER_ID:self.userInfo.userId};
    
    __unsafe_unretained HOTMainViewController *tempObj = self;
    [self requestWithURL:HOT_URL_FIND_BALANCE Parameters:parameters success:^(NSInteger result, NSDictionary * data) {
        
        [tempObj loadingSuccess];
        
        _balanceLabel.text = [HOTUtils getString:[data objectForKey:@"user_balance"]];
        
        
    } failure:^(NSError *error, NSString *errMsg) {
        
        [tempObj loadingSuccess];
        if ([tempObj checkErrResult:error errMsg:errMsg]) {
            return ;
        }
                if (errMsg.length > 0) {
        
                    ZZZPopupView *popupView = [[ZZZPopupView alloc] initWithTitle:ALERT_TITLE
                                                                          message:errMsg
                                                                         delegate:nil
                                                                cancelButtonTitle:nil
                                                                 otherButtonTitle:ALERT_BTN_OK];
        
                    [popupView showInWindow];
        }
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark- button clicked
- (IBAction)profileSettingBtnClicked:(id)sender {
    
    HOTProfileSettingViewController *profileVc = [[HOTProfileSettingViewController alloc] initWithNibName:@"HOTProfileSettingViewController" bundle:nil];
    
    [self.navigationController pushViewController:profileVc animated:YES];
}

- (IBAction)memoBtnClicked:(id)sender {
    
    HOTMemoViewController *memoVc = [[HOTMemoViewController alloc] initWithNibName:@"HOTMemoViewController" bundle:nil];
    [self.navigationController pushViewController:memoVc animated:YES];
}

- (IBAction)contributionHistoryBtnClicked:(id)sender {
    
    HOTContributionHistoryListViewController *cbhVC = [[HOTContributionHistoryListViewController alloc] initWithNibName:@"HOTContributionHistoryListViewController" bundle:nil];
    
    [self.navigationController pushViewController:cbhVC animated:YES];
}

- (IBAction)buyNowBtnClicked:(id)sender {
    
    BOOL isFree = [HOTConfig config].isFree;
    
    if (isFree) {
        
        //免费时，提示
        ZZZPopupView *popupView = [[ZZZPopupView alloc] initWithTitle:ALERT_TITLE message:MSG_CAN_NOT_TO_PHASE delegate:nil cancelButtonTitle:nil otherButtonTitle:ALERT_BTN_OK];
        
        [popupView showInWindow];
        
    } else {
        HOTStoreViewController *svc = [[HOTStoreViewController alloc] initWithNibName:@"HOTStoreViewController" bundle:nil];
        [self.navigationController pushViewController:svc animated:YES];
    }
    
    
}
- (IBAction)messageBoxBtnClicked:(id)sender {
    
    HOTMessageBoxViewController *msgBoxVc = [[HOTMessageBoxViewController alloc] initWithNibName:@"HOTMessageBoxViewController" bundle:nil];
    
    [self.navigationController pushViewController:msgBoxVc animated:YES];
}

-(void)settingBtnClicked {
    
    HOTSettingViewController *svc = [[HOTSettingViewController alloc] initWithNibName:@"HOTSettingViewController" bundle:nil];
    
    [self.navigationController pushViewController:svc animated:YES];
}
@end
